package com.lx.demo.service.demo;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.codingapi.tx.annotation.TxTransaction;
import com.lx.MsagesException;
import com.lx.Remessages;
import com.lx.Request;
import com.lx.Response;
import com.lx.demo.b.interfaces.TestDemoBDubboService;
import com.lx.demo.b.interfaces.dto.PersonTestInfoDTO;
import com.lx.demo.dal.entity.Orders;
import com.lx.demo.dal.mapper.OrdersMapper;
import com.lx.demo.interfaces.TestDubboDemoService;
import com.lx.demo.interfaces.dto.TestDubboRequestDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * Created by lanyxp on 2021/2/23 17:12
 */

@Slf4j
@RequiredArgsConstructor
@com.alibaba.dubbo.config.annotation.Service
public class TestDubboDemoServiceImpl implements TestDubboDemoService {

    private final OrdersMapper ordersMapper;

    private final TestDemoBDubboService testDemoBDubboService;

    /***
     * dubbo 服务端测试@TxTransaction & @Transactional
     * @param req 请求参数
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @TxTransaction
    @Override
    public Response addDataTest(Request<TestDubboRequestDTO> req) {
        log.info("开始请求DUBOO服务>>>>请求参数:{}", JSON.toJSONString(req));
        Orders orders = new Orders();
        orders.setOrderId(req.getBody().getCode());
        orders.setCode(req.getBody().getCode());
        orders.setCreateTime(new Date());
        orders.setUpdateTime(new Date());
        int results = this.ordersMapper.insert(orders);
        PersonTestInfoDTO personTestInfoDTO = new PersonTestInfoDTO();
        personTestInfoDTO.setAge(100);
        personTestInfoDTO.setName("chenjiang");
        Request<PersonTestInfoDTO> personTestInfoDTORequest = Remessages.CreateRequest(personTestInfoDTO);
        final Response response = testDemoBDubboService.insertIntoDB(personTestInfoDTORequest);
        if (!response.isSuccess()) {
            throw new MsagesException("dubbo服务调用异常");
        }
        log.info("开始请求DUBOO服务>>>>返回结果:{}", JSON.toJSONString(Remessages.success(results)));
        return Remessages.success(results);
    }
}
